let express=require("express")()
let mysql=require("mysql")
let expressStatic=require("express-static")
let port=8080
let sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'manage',
  timezone : "08:00"
});
sql.connect()

express.get("/editstus",(request,response)=>{
	sql.query(`UPDATE students SET sex="${request.query.sex}",name="${request.query.name}",age="${request.query.age}",city="${request.query.city}",joinDate="${request.query.joinDate}" WHERE id="${request.query.id}"`,(error,data)=>{
		if(error){
			console.log(error)
			response.send(`编辑学生失败`)
		}
		else{
			
			response.send(`编辑学生成功`)
		}
	})
})




express.get("/addstus",(request,response)=>{
	sql.query(`INSERT INTO students (name,age,joinDate,sex,city) VALUES ("${request.query.name}","${request.query.age}","${request.query.joinDate}","${request.query.sex}","${request.query.city}")`,(error,data)=>{
		
		if(error){
			console.log(error)
			response.send(`添加学生失败`)
		}
		else{
			response.send(`添加学生成功`)
		}
	})
})

express.get("/deletestus",(request,response)=>{
	sql.query(`DELETE FROM students WHERE id=${request.query.id}`,(error,data)=>{
		if(error){
			console.log(error)
			response.send(`删除学生失败`)
		}
		else{
			response.send(`删除学生成功`)
			
		}
	})
})

express.get("/getstus",(request,response)=>{
	sql.query(`SELECT * FROM students`,(error,data)=>{
		if(error){
			console.log(error)
			response.send("error")
		}
		else{
			response.send(data)
			console.log(data)
		}
	} )
})




express.use(expressStatic(__dirname + '/static'))
express.listen(port)
